package com.zmx.job.admin.service;

import com.zmx.job.admin.entity.MallJobInfo;
import com.zmx.job.admin.repository.MallJobInfoRepository;
import com.zmx.job.admin.utils.CronUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @author zhaomxr
 */
@Service
public class MallJobInfoService {

    @Autowired
    private MallJobInfoRepository mallJobInfoRepository;

    public List<MallJobInfo> getJobInfo(String name){
        List<MallJobInfo> mallJobInfos = mallJobInfoRepository.findAll();
        return mallJobInfos;
    }

    /**
     * 新增任务
     * @param mallJobInfo
     * @return
     */
    public MallJobInfo save(MallJobInfo mallJobInfo) {
        if (mallJobInfo == null) {
            throw new RuntimeException("保存失败");
        }

        MallJobInfo executorHandler = mallJobInfoRepository.findByExecutorHandler(mallJobInfo.getExecutorHandler());
        if (executorHandler != null) {
            throw new RuntimeException("执行器任务handler已存在");
        }

        mallJobInfo.setTriggerStatus(1);
        mallJobInfo.setTriggerLastTime(CronUtil.getLastTriggerTime(mallJobInfo.getScheduleConf()));
        mallJobInfo.setTriggerNextTime(CronUtil.getNextTriggerTime(mallJobInfo.getScheduleConf()));
        mallJobInfo.setAddTime(new Date());
        MallJobInfo save = mallJobInfoRepository.save(mallJobInfo);
        return save;
    }

    /**
     * 更新任务
     * @param mallJobInfo
     * @return
     */
    public MallJobInfo update(MallJobInfo mallJobInfo) {
        if (mallJobInfo == null || mallJobInfo.getId() == null) {
            throw new RuntimeException("更新失败");
        }

        MallJobInfo executorHandler = mallJobInfoRepository.findByExecutorHandler(mallJobInfo.getExecutorHandler());

        mallJobInfo.setTriggerStatus(1);
        mallJobInfo.setTriggerLastTime(CronUtil.getLastTriggerTime(mallJobInfo.getScheduleConf()));
        mallJobInfo.setTriggerNextTime(CronUtil.getNextTriggerTime(mallJobInfo.getScheduleConf()));
        mallJobInfo.setUpdateTime(new Date());
        MallJobInfo save = mallJobInfoRepository.save(mallJobInfo);
        return save;
    }

    /**
     * 启用任务
     * @param id
     * @return
     */
    public MallJobInfo start(Long id) {
        if (id == null || id <= 0) {
            throw new RuntimeException("启用失败");
        }

        Optional<MallJobInfo> jobInfo = mallJobInfoRepository.findById(String.valueOf(id));
        MallJobInfo mallJobInfo = jobInfo.get();

        mallJobInfo.setTriggerStatus(1);
        mallJobInfo.setTriggerLastTime(CronUtil.getLastTriggerTime(mallJobInfo.getScheduleConf()));
        mallJobInfo.setTriggerNextTime(CronUtil.getNextTriggerTime(mallJobInfo.getScheduleConf()));
        mallJobInfo.setUpdateTime(new Date());
        MallJobInfo save = mallJobInfoRepository.save(mallJobInfo);
        return save;
    }

    /**
     * 停用任务
     * @param id
     * @return
     */
    public MallJobInfo stop(Long id) {
        if (id == null || id <= 0) {
            throw new RuntimeException("停用失败");
        }

        Optional<MallJobInfo> jobInfo = mallJobInfoRepository.findById(String.valueOf(id));
        MallJobInfo mallJobInfo = jobInfo.get();

        mallJobInfo.setTriggerStatus(0);
        mallJobInfo.setTriggerLastTime(0L);
        mallJobInfo.setTriggerNextTime(0L);
        mallJobInfo.setUpdateTime(new Date());
        MallJobInfo save = mallJobInfoRepository.save(mallJobInfo);
        return save;
    }
}
